package com.ruoyi.education.service.impl;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.List;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.education.domain.Classes;
import com.ruoyi.education.domain.Student;
import com.ruoyi.education.service.IClassesService;
import com.ruoyi.education.service.IStudentService;
import com.ruoyi.education.service.ITeacherService;
import com.ruoyi.education.utils.AESUtil;
import com.ruoyi.education.utils.Base58Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.education.mapper.AssistMapper;
import com.ruoyi.education.domain.Assist;
import com.ruoyi.education.service.IAssistService;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

/**
 * 帮扶管理Service业务层处理
 * 
 * @author imachen
 * @date 2022-10-25
 */
@Service
public class AssistServiceImpl implements IAssistService 
{
    @Autowired
    private AssistMapper assistMapper;

    @Autowired
    private IStudentService studentService;

    @Autowired
    private IClassesService classesService;

    @Autowired
    private ITeacherService teacherService;

    /**
     * 查询帮扶管理
     * 
     * @param id 帮扶管理主键
     * @return 帮扶管理
     */
    @Override
    public Assist selectAssistById(Long id)
    {
        return assistMapper.selectAssistById(id);
    }

    /**
     * 查询帮扶管理列表
     * 
     * @param assist 帮扶管理
     * @return 帮扶管理
     */
    @Override
    public List<Assist> selectAssistList(Assist assist)
    {
        return assistMapper.selectAssistList(assist);
    }

    /**
     * 新增帮扶管理
     * 
     * @param assist 帮扶管理
     * @return 结果
     */
    @Override
    public int insertAssist(Assist assist){
        //如果改学生在系统中不存在，那么根据录入的信息自动添加一个学生信息
        String studentName = assist.getStudentName();
        String teacherName = assist.getTeacherName();
        String className = assist.getClassName();
        Student student = studentService.selectStudentByName(studentName);
        if ( student == null) {
            student = new Student();
            student.setClassName(className);
            student.setStuPhone(assist.getStuPhone());
            student.setStudentName(studentName);
            studentService.insertStudent(student);
        }
        //帮扶老师分配算法
        //选择了意向老师，直接把任务分配给意向老师

        //如果没有选择意向老师，选择了班级，任务分配给该班级的技术老师
        if (StringUtils.isEmpty(teacherName) && !StringUtils.isEmpty(className) ){
            // 根据班级名查询班级信息
            Classes classes = classesService.selectClassesByName(className);
            // 把任务分配给该班级的技术老师
            assist.setTeacherName(classes.getAssistantTeacher());
        }
        //如果没有选择意向老师也没有选择班级，查询所有的技术老师，随机分配一个技术老师
        else if (StringUtils.isEmpty(teacherName) && StringUtils.isEmpty(className)) {
            // 查询所有的技术老师
            List<String> technicalTeachers = teacherService.selectTeacherListByType(1);
            // 随机分配一个技术老师
            int index = (int) (Math.random()* technicalTeachers.size());
            assist.setTeacherName(technicalTeachers.get(index));
        }
        // 设置状态 保存帮扶预约信息
        assist.setStatus(0);
        int insertAssist = assistMapper.insertAssist(assist);

        // 发送一个提醒邮件给该帮扶任务的技术老师
        System.out.println(assist.getTeacherName() +"老师，学生 [" + studentName + "] 需要你的帮助");

        return insertAssist;
    }

    /**
     * 修改帮扶管理
     * 
     * @param assist 帮扶管理
     * @return 结果
     */
    @Override
    public int updateAssist(Assist assist) throws Exception {
        int updateAssist = assistMapper.updateAssist(assist);
        // 使用AES加密解密对帮扶预约ID进行加密
        byte[] bytes = AESUtil.encryptId(assist.getId());
        String code = Base58Util.encode(bytes);

        if ( assist.getStatus() == 1 || assist.getStatus() == 2) {
            // 往学生邮箱发送一封评价邮件
            String url = "http://localhost/evaluate?code=" + code;
            System.out.println(assist.getStudentName() + " 你好,对于["+ assist.getAppointmentTime() +"]的问题是否得到解决？" +
                    "请访问网址[" + url + "]对此次服务做出评价");
        }

        return updateAssist;
    }

    /**
     * 批量删除帮扶管理
     * 
     * @param ids 需要删除的帮扶管理主键
     * @return 结果
     */
    @Override
    public int deleteAssistByIds(Long[] ids)
    {
        return assistMapper.deleteAssistByIds(ids);
    }

    /**
     * 删除帮扶管理信息
     * 
     * @param id 帮扶管理主键
     * @return 结果
     */
    @Override
    public int deleteAssistById(Long id)
    {
        return assistMapper.deleteAssistById(id);
    }

    @Override
    public Assist getAssistByCode(Long id) throws Exception {
        {
            return assistMapper.selectAssistById(id);
        }
    }
}
